<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\inter\model;


use my\Security;
use think\Db;
use think\Model;

class UserModel extends Model
{


    public function __construct()
    {
        parent::__construct();

    }

    public function getUserByUid($uid, $string)
    {
        return Db::connect(config('inner_db'))->name("user")->field($string)->where(array('uid'=>$uid))->find();
    }

    public function getUserByPhone($phone, $string)
    {
        return Db::connect(config('inner_db'))->name("user")->field($string)->where(array('user_phone'=>Security::encrypt($phone)))->find();
    }
    /**
     * 设置密码、忘记密码
     *
     **/
    public function setPass($user_phone, $user_pass){
        return Db::connect(config('inner_db'))->name("user")->where(array('user_phone'=>$user_phone))->setField('user_pass', $user_pass);

    }
    /**
     * 注册时添加信息
     *
     * @var string
     **/
    public function createUser($data)
    {
      
        $data['user_pass']=md5($data['user_pass']);
        $data['register_time']=time();
        $data['token']=md5(date("YmdHis",time()).rand(10000,99999));
        //***判断手机号码是否已经存在 
        $data['user_phone'] = Security::encrypt($data['user_phone']);
        $data['uid'] =  Db::connect(config('inner_db'))->name('user')->insertGetId($data);
        //****注册成功后，往user_wallet用户插入一天数据
        $user_wallet = Db::connect(config('inner_db'))->name('user_wallet')->where('uid',$data['uid'])->find();
        if(!$user_wallet){
            $wallet_data['uid'] = $data['uid'];
            $wallet_data['wallet_money'] = 0;
            $wallet_data['time'] = time();
            Db::connect(config('inner_db'))->name('user_wallet')->insertGetId($wallet_data);
        }
        if($data['uid']){
           return $data;
        }else{
           return false;
        }
        // $user_phone = $arr['user_phone'];
    }

    /**
     * 添加用户日志
     *
     **/
    public function createUserLog($data){
        return Db::connect(config('inner_db'))->name('login_log_user')->insertGetId($data);
    }
    public function UpdateUser($data)
    {
        Db::connect(config('inner_db'))->name('user')->update($data);
    }

    public function getSign($uid)
    {
        $where['record_time']=date('Ymd',time());
        $where['uid']=$uid;
        return Db::connect(config('inner_db'))->name("sign")->field("id")->where($where)->find();
    }

    public function sign($uid)
    {

        Db::startTrans();
        try{
            $data['record_time']=date('Ymd',time());
            $data['uid']=$uid;
            $data['add_time']=time();
            $data['money']=rand(1,50);
            Db::connect(config('inner_db'))->name("sign")->insert($data);
            $walletModel=new WalletModel();
            $walletModel->addWallet($uid,$data['money']);
            Db::commit();
            return $data['money']/100;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }


    }
    /**
     * 处理用户点击确定后的余额和行为消费
     * @var string
     * @var int
     * @var int
     * @int int 1 纯余额 2 纯支付宝 3 纯微信 4余额+支付宝 5 余额+微信
     * @int string
     * @int string
     **/

    public function setUserMoney($order_sn,$common_id,$uid,$type,$third_pay,$wallet_pay){
        //1更新order_common表，order_status=45已付款
        Db::connect(config('inner_db'))->name('order_common')->where('common_id',$common_id)->update(['order_pay'=>$type,'order_status'=>45,'user_money'=>$wallet_pay]);
        //2如果wallet_pay不等于0
        if($wallet_pay > 0){
            Db::connect(config('inner_db'))->name('user_wallet')->where('uid', $uid)->setDec('wallet_money', $wallet_pay);
        }
        //3添加一个用户流水
        $data['type'] = $type;//纯余额
        $data['uid'] = $uid;
        $data['money'] = $third_pay + $wallet_pay;
        $data['time'] = time();
        $data['status'] = 1;
        $data['money_statement'] = 2;//消费付款
 //       $data['out_trade_no'] = $order_sn;//消费付款
        $data['common_id'] = $common_id;//消费付款
        Db::connect(config('inner_db'))->name('user_recharge')->insert($data);
    }
  

}